<!-- <template>
    <div style="border: 1px solid #ccc;">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 500px; overflow-y: hidden;"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
    </div>
</template>
<script>

// import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

// export default Vue.extend({
//     components: { Editor, Toolbar },
//     data() {
//         return {
//             editor: null,
//             html: '<p>hello</p>',
//             toolbarConfig: { },
//             editorConfig: { placeholder: '请输入内容...' },
//             mode: 'default', // or 'simple'
//         }
//     },
//     methods: {
//         onCreated(editor) {
//             this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
//         },
//     },
//     mounted() {
//         // 模拟 ajax 请求，异步渲染编辑器
//         setTimeout(() => {
//             this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
//         }, 1500)
//     },
//     beforeDestroy() {
//         const editor = this.editor
//         if (editor == null) return
//         editor.destroy() // 组件销毁时，及时销毁编辑器
//     }
// })
</script> -->

<template>
  <div>
    <div style="border: 1px solid #ccc; margin-top: 10px">
      <Toolbar
        :mode="mode"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        style="border-bottom: 1px solid #ccc"
      />
      <Editor
        :mode="mode"
        :defaultConfig="editorConfig"
        v-model="valueHtml"
        style="height: 300px; overflow-y: hidden"
        @onCreated="handleCreated"
        @onChange="handleChange"
        @onDestroyed="handleDestroyed"
        @onFocus="handleFocus"
        @onBlur="handleBlur"
        @customAlert="customAlert"
        @customPaste="customPaste"
      />
    </div>
  </div>
</template>

<script setup>
import "@wangeditor/editor/dist/css/style.css";
import { onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const props = defineProps({
  content: {
    type: String,
    default: "",
  },
});
const emits = defineEmits([
    "handleChange",
]);

// 编辑器实例，必须用 shallowRef，重要！
const editorRef = shallowRef();
const mode = "default";

// 内容 HTML
const valueHtml = ref("");
const toolbarConfig = {};
const editorConfig = { placeholder: "请输入内容...", MENU_CONF: {} };
// 配置上传图片
editorConfig.MENU_CONF["uploadImage"] = {
  server: uploadimgUrl,
  meta: {
    project_code: "mobybox",
  },
  fieldName: "file",
  // 自定义增加 http  header
  headers: {
    ...TOKENT_HEADER,
  },
  // 自定义插入图片
  customInsert(res, insertFn) {
    // console.log(res.data.pic_url)

    insertFn(res.data.pic_url, res.data.name, "");
  },
};
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = props.content;
  }, 50);
});

// 组件销毁时，也及时销毁编辑器，重要！
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;

  editor.destroy();
});

// 编辑器回调函数
const handleCreated = (editor) => {
  console.log("created", editor);
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const handleChange = (editor) => {
  emits("handleChange", editor.getHtml());
  // console.log("change:", editor.getHtml());
};
const handleDestroyed = (editor) => {
  console.log("destroyed", editor);
};
const handleFocus = (editor) => {
  emits("handleChange", editor.getHtml());
  console.log("focus", editor);
};
const handleBlur = (editor) => {
  emits("handleChange", editor.getHtml());
  console.log("blur", editor);
};
const customAlert = (info, type) => {
  alert(`【自定义提示】${type} - ${info}`);
};
const customPaste = (editor, event, callback) => {
  callback(true); // 返回 false ，阻止默认粘贴行为
};
</script>
